home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / utility / cenvid / winini.lib < prev    next >
Encoding:
Text File  |  1994-08-25  |  8.2 KB  |  230 lines

  1. // WinIni.lib - Cmm routines to interface with Windows' Profile
  2. // ver.1        (i.e. *.INI) files from DOS.
  3. //
  4. // NOTE: This should not be called from CEnvi for Windows
  5. //
  6. //**** GetIniString(): return key value from .ini file
  7. // SYNTAX: string GetIniString(string FileName,string AppName,string KeyName)
  8. // WHERE: FileName: name of profile (*.INI) file
  9. //        AppName: Application name ([AppName] in .ini files)
  10. //        KeyName: Key name under [AppName] in the .ini file
  11. // RETURN: Return the profile string if found; else return NULL if not
  12. //         found or if file cannot be read
  13. //
  14. //
  15. //**** WriteIniString(): write key value to .ini file
  16. // SYNTAX: bool WriteIniString(string FileName,string AppName,string KeyName,
  17. //                             string Value[,bool Replace])
  18. // WHERE: FileName: name of profile file
  19. //        AppName: Application name ([AppName] in .ini files)
  20. //        KeyName: Key name under [AppName] in the .ini file; if NULL then
  21. //                 deletes the entire application section
  22. //        Value: String to set KeyName equal to in the .INI file; if NULL
  23. //               then delete this key
  24. //        Replace: Optional (True if not supplied) to Replace any existing key
  25. //                 with this value, else replaces the key with this value
  26. // RETURN: Return FALSE if failure, else success
  27. //
  28. //
  29. //**** GetIniAppList(): Get array of all Application names in a profile file
  30. // SYNTAX: string[] GetIniAppList(string FileName)
  31. // WHERE: FileName: name of profile file
  32. // RETURN: Returns array of all AppNames in ini file; NULL if none found
  33. //
  34. //
  35. //**** GetIniKeyList(): Get array of all keys in a profile file for Application
  36. // SYNTAX: string[] GetProfileKeyList(string FileName,string AppName)
  37. // WHERE: FileName: name of profile file
  38. //        AppName: Application name ([AppName] in .ini files)
  39. // RETURN: Returns array of all keys under AppName; NULL if none found
  40. //           Each element of array has following two values:
  41. //                .Key     KeyName string
  42. //                .Value   Value string
  43. //
  44. //
  45.  
  46.  
  47. GetIniString(pFileName,pAppName,pKeyName)
  48. {
  49.    if ( lList = GetIniKeyList(pFileName,pAppName) ) {
  50.       lKeyCount = 1 + GetArraySpan(lList);
  51.       for ( lIdx = 0; lIdx < lKeyCount; lIdx++ ) {
  52.          if ( !stricmp(pKeyName,lList[lIdx].Key) )
  53.             return lList[lIdx].Value;
  54.       }
  55.    }
  56.    return NULL;
  57. }
  58.  
  59. WriteIniString(pFileName,pAppName,pKeyName,pValue,pReplace)
  60. {
  61.    lAppNameLen = strlen(pAppName);
  62.  
  63.    // read file
  64.    if ( !ReadIniFile(pFileName,lIniList) )
  65.       return False;
  66.  
  67.    // locate the section in the IniFile for pAppName
  68.    lAppMax = GetArraySpan(lIniList);
  69.    for ( lAppIdx = 1; lAppIdx <= lAppMax; lAppIdx++ ) {
  70.       if ( !strnicmp(pAppName,lIniList[lAppIdx].App,lAppNameLen) )
  71.          break;
  72.    }
  73.    if ( lAppIdx <= lAppMax ) {
  74.       if ( !pKeyName ) {
  75.          // pKeyName is NULL, and so delete this section
  76.          undefine( lIniList[lAppIdx].Key );
  77.       } else {
  78.          lKeyNameLen = sprintf(lKeyName,"%s=",pKeyName);
  79.          // application section was found; if replace and found then overwrite
  80.          // else write at the end.
  81.          lKeyMax = GetArraySpan(lKeys = lIniList[lAppIdx].Key);
  82.          lReplaced = (pValue == NULL);
  83.          if ( va_arg() < 5  ||  pReplace ) {
  84.             for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
  85.                if ( !strnicmp(lKeyName,lKeys[lIdx],lKeyNameLen) ) {
  86.                   if ( !pValue ) {
  87.                      // delete this key
  88.                      lKeys[lIdx] = NULL;
  89.                   } else {
  90.                      sprintf(lKeys[lIdx],"%s=%s\n",pKeyName,pValue);
  91.                      lReplaced = True;
  92.                   }
  93.                }
  94.             }
  95.          }
  96.          if ( !lReplaced ) {
  97.             // add this value to the end of keys
  98.             lLast = lKeys[lKeyMax];
  99.             if ( lLast[0] && lLast[0] != '\n' )
  100.                lLast = lKeys[++lKeyMax];
  101.             sprintf(lLast,"%s=%s\n",pKeyName,pValue);
  102.          }
  103.       }
  104.    } else {
  105.       // application section was not found, so add a new application section
  106.       strcpy(lIniList[lAppMax+1].App,pAppName);
  107.       sprintf(lIniList[lAppMax+1].Key[0],"[%s]\n",pAppName);
  108.       sprintf(lIniList[lAppMax+1].Key[1],"%s=%s\n",pKeyName,pValue);
  109.    }
  110.    return WriteIniFile(pFileName,lIniList);
  111. }
  112.  
  113.  
  114. //GetIniString(pFileName,pAppName,pKeyName)
  115. //{
  116. //   lAppNameLen = strlen(pAppName);
  117. //   lKeyNameLen = sprintf(lKeyName,"%s=",pKeyName);
  118. //   if ( ReadIniFile(pFileName,lIniList) ) {
  119. //      lAppMax = GetArraySpan(lIniList);
  120. //      for ( lAppIdx = 1; lAppIdx <= lAppMax; lAppIdx++ ) {
  121. //         if ( !strnicmp(pAppName,lIniList[lAppIdx].App,lAppNameLen) )
  122. //            break;
  123. //      }
  124. //      if ( lAppIdx <= lAppMax ) {
  125. //         // Application section was found; check each line
  126. //         lKeyMax = GetArraySpan(lKey = lIniList[lAppIdx].Key);
  127. //         for ( lKeyIdx = 1; lKeyIdx <= lKeyMax; lKeyIdx++ ) {
  128. //            if ( !strnicmp(lKeyName,lKey[lKeyIdx],lKeyNameLen) ) {
  129. //               strcpy(lValue,lKey[lKeyIdx]+lKeyNameLen);
  130. //               if ( lValue[strlen(lValue)-1] == '\n' )
  131. //                  lValue[strlen(lValue)-1] = '\0';
  132. //               return lValue;
  133. //            }
  134. //         }
  135. //      }
  136. //   }
  137. //   // never found string, so return NULL
  138. //   return NULL;
  139. //}
  140.  
  141. GetIniAppList(pFileName)
  142. {
  143.    if ( !ReadIniFile(pFileName,lIniList) )
  144.       return NULL;
  145.    if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
  146.       return NULL;
  147.    for ( lIdx = 1; lIdx <= lAppMax; lIdx++ )
  148.       strcpy(lList[lIdx-1],lIniList[lIdx].App);
  149.    return lList;
  150. }
  151.  
  152. GetIniKeyList(pFileName,pAppName)
  153. {
  154.    if ( !ReadIniFile(pFileName,lIniList) )
  155.       return NULL;
  156.    if ( (lAppMax = GetArraySpan(lIniList)) < 1 )
  157.       return NULL;
  158.    for ( lIdx = 1; lIdx <= lAppMax; lIdx++ ) {
  159.       if ( !stricmp(pAppName,lIniList[lIdx].App) )
  160.          break;
  161.    }
  162.    if ( lIdx <= lAppMax ) {
  163.       lKeyList = lIniList[lIdx].Key;
  164.       lCount = 0;
  165.       lKeyMax = GetArraySpan(lKeyList);
  166.       for ( lIdx = 1; lIdx <= lKeyMax; lIdx++ ) {
  167.          lString = lKeyList[lIdx];
  168.          if ( lString[0] != ' ' && lString[0] != ';'  && (lEqual = strchr(lString,'=')) ) {
  169.             if ( lLen = lEqual - lString ) {
  170.                strncpy(lList[lCount].Key,lString,lLen+1);
  171.                lList[lCount].Key[lLen] = '\0';
  172.                strcpy(lValue,lEqual+1);
  173.                if ( lValue[strlen(lValue)-1] == '\n' )
  174.                   lValue[strlen(lValue)-1] = '\0';
  175.                strcpy(lList[lCount].Value,lValue);
  176.                lCount++;
  177.             }
  178.          }
  179.       }
  180.       if ( !lCount )
  181.          return NULL;
  182.    }
  183.    return lList;
  184. }
  185.  
  186. ReadIniFile(pFileName,pIniList)  // set ini array, return False if cannot
  187. {                                // read file
  188.    undefine(pIniList); // undo any previos settings
  189.    if ( !(lFp = fopen(pFileName,"rt")) )
  190.       return False;
  191.    // pretend initial app is blank
  192.    pIniList[lAppIdx = 0].App = "";
  193.    lKeyIdx = -1;
  194.    while ( lLine = fgets(lFp) ) {
  195.       if ( lLine[0] == '['  &&  strchr(lLine+1,']') ) {
  196.          // this is a new application name
  197.          lEnd = strchr(strcpy(pIniList[++lAppIdx].App,lLine+1),']');
  198.          lEnd[0] = '\0';
  199.          pIniList[lAppIdx].Key[lKeyIdx=0] = lLine;
  200.       } else {
  201.          // this is another line in the application
  202.          pIniList[lAppIdx].Key[++lKeyIdx] = lLine;
  203.       }
  204.    }
  205.    fclose(lFp);
  206.    return True;
  207. }
  208.  
  209. WriteIniFile(pFileName,pIniList) // write out ini file; false if error
  210. {
  211.    if ( !(lFp = fopen(pFileName,"wt")) )
  212.       return False;
  213.    lAppMax = GetArraySpan(pIniList);
  214.    for ( lAppIdx = 0; lAppIdx <= lAppMax; lAppIdx++ ) {
  215.       if ( defined(pIniList[lAppIdx].Key) ) {
  216.          lKeyMax = GetArraySpan(pIniList[lAppIdx].Key);
  217.          for ( lKeyIdx = 0; lKeyIdx <= lKeyMax; lKeyIdx++ ) {
  218.                if ( lLine = pIniList[lAppIdx].Key[lKeyIdx] )
  219.                   fwrite(lLine,strlen(lLine),lFp);
  220.          }
  221.          // if the last line wasn't a blank, then write a blank line
  222.          if ( !lLine  || (lLine[0] && lLine[0] != '\n') )
  223.             fprintf(lFp,"\n");
  224.       }
  225.    }
  226.    fclose(lFp);
  227.    return True;
  228. }
  229.  
  230.